package com.rongx.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongx.reggie.common.CustomException;
import com.rongx.reggie.entity.Category;
import com.rongx.reggie.entity.Dish;
import com.rongx.reggie.entity.Setmeal;
import com.rongx.reggie.mapper.CategoryMapper;
import com.rongx.reggie.service.CategoryService;
import com.rongx.reggie.service.DishService;
import com.rongx.reggie.service.SetmealService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分类服务实现类
 * 提供分类管理的业务逻辑处理
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    private final DishService dishService;
    private final SetmealService setmealService;

    /**
     * 构造器注入依赖服务
     *
     * @param dishService 菜品服务
     * @param setmealService 套餐服务
     */
    public CategoryServiceImpl(DishService dishService, SetmealService setmealService) {
        this.dishService = dishService;
        this.setmealService = setmealService;
    }

    /**
     * 根据id删除分类，删除之前需要进行判断
     * 检查分类是否关联了菜品或套餐，如有关联则抛出异常
     *
     * @param id 分类ID
     * @throws CustomException 当分类关联了菜品或套餐时抛出
     */
    @Override
    public void remove(Long id) {
        //添加查询条件，根据分类id进行查询菜品数据
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        //如果已经关联，抛出一个业务异常
        if(count1 > 0){
            throw new CustomException("当前分类下关联了菜品，不能删除");//已经关联菜品，抛出一个业务异常
        }

        //查询当前分类是否关联了套餐，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if(count2 > 0){
            throw new CustomException("当前分类下关联了套餐，不能删除");//已经关联套餐，抛出一个业务异常
        }

        //正常删除分类
        super.removeById(id);
    }

    /**
     * 分页查询分类信息
     *
     * @param page 页码
     * @param pageSize 每页大小
     * @return 分类分页数据
     */
    @Override
    public Page<Category> setPage(int page, int pageSize) {
        Page<Category> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        this.page(pageInfo, queryWrapper);
        return pageInfo;
    }

    /**
     * 根据条件查询分类列表
     *
     * @param category 查询条件
     * @return 分类列表
     */
    @Override
    public List<Category> getList(Category category) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType() != null, Category::getType, category.getType());
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
         return this.list(queryWrapper);
    }
}
